package com.yuexunit.mobilenurse.module.DocAdvice.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.module.DocAdvice.bean.DocAdvice_Medicine;
import com.yuexunit.mobilenurse.module.DocAdvice.ui.fragment.Frag_Medicine;
import com.yuexunit.mobilenurse.module.DocAdvice.ui.fragment.Frag_OutMedicine;

import org.kymjs.kjframe.ui.ViewInject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/5/3 0003.
 */

public class OutMedicineListAdapter extends BaseAdapter {
    private Context context;
    private Frag_OutMedicine activity;
    private ArrayList<DocAdvice_Medicine> mDatas;

    public OutMedicineListAdapter(Frag_OutMedicine activity, Context context, ArrayList<DocAdvice_Medicine> mDatas){
        this.context = context;
        this.activity =activity;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return null == mDatas ? 0 : mDatas.size();
//        return 7;
    }

    @Override
    public Object getItem(int position) {
        return null == mDatas ? null : mDatas.get(position);
//        return null;
    }

    @Override
    public long getItemId( int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final OutMedicineListAdapter.ViewHolder holder;
        if(null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.outmedicinelist_item, parent, false);
            holder = new OutMedicineListAdapter.ViewHolder();
            holder.injectionlist_list = (ListView)convertView.findViewById(R.id.injectionlist_list);
            holder.injectionlist_statu_btn = (Button)convertView.findViewById(R.id.injectionlist_statu_btn);
            holder.injectionlist_plan_usage = (TextView)convertView.findViewById(R.id.injectionlist_plan_usage);
            holder.injectionlist_statu_tv = (TextView)convertView.findViewById(R.id.injectionlist_statu_tv);
            holder.injectionlist_count = (TextView)convertView.findViewById(R.id.injectionlist_count);
            holder.injectionlist_ll = (RelativeLayout)convertView.findViewById(R.id.injectionlist_ll);
            holder.radiobutton = (CheckBox) convertView.findViewById(R.id.radiobutton);
            convertView.setTag(holder);
        } else {
            holder = (OutMedicineListAdapter.ViewHolder)convertView.getTag();
        }
//        holder.injectionlist_statu_btn.setText("开始发药");
//        holder.injectionlist_plan_usage.setText(mDatas.get(position).getDocAdvice_orderlist().getExecfreq() + "  "+ mDatas.get(position).getDocAdvice_orderlist().getExecway());
        holder.injectionlist_plan_usage.setText(mDatas.get(position).getDocAdvice_orderlist().getExecfreq());
        holder.injectionlist_statu_tv.setText(mDatas.get(position).getStatus());
        holder.injectionlist_count.setText(mDatas.get(position).getCount());

        if(mDatas.get(position).getStatus().equals("等待发药"))
        {
            holder.injectionlist_ll.setBackgroundResource(R.color.white);
//            holder.injectionlist_ll.setBackgroundResource(R.color.white);
//            holder.injectionlist_statu_tv.setTextColor(context.getResources().getColor(R.color.doc_advice_01));
//            holder.injectionlist_statu_btn.setVisibility(View.VISIBLE);
//            holder.injectionlist_statu_btn.setEnabled(true);
            holder.radiobutton.setVisibility(View.VISIBLE);
        }
        else
        {
            holder.injectionlist_ll.setBackgroundResource(R.color.doc_advice_04);
//            holder.injectionlist_ll.setBackgroundResource(R.color.doc_advice_04);
//            holder.injectionlist_statu_tv.setTextColor(context.getResources().getColor(R.color.doc_advice_03));
//            holder.injectionlist_statu_btn.setVisibility(View.INVISIBLE);
//            holder.injectionlist_statu_btn.setEnabled(false);
            holder.radiobutton.setVisibility(View.GONE);
        }

        holder.radiobutton.setOnClickListener(new CheckBox.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( holder.radiobutton.isChecked()) {
                    activity.select_order(position,true);
                } else {
                    activity.select_order(position,false);
                }
            }
        });

        if (mDatas.get(position).isCheck()) {
            holder.radiobutton.setChecked(true);
        } else {
            holder.radiobutton.setChecked(false);
        }


        BaseAdapter adapter  = new OutMedicineListListAdapter(context,mDatas.get(position).getDespensing_detail());
        holder.injectionlist_list.setAdapter(adapter);
//        holder.injectionlist_statu_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String barcode ="-1";
//                for(int i = 0;i<mDatas.get(position).getDespensing_exec().size();i++)
//                {
//                    if(((Integer.valueOf(barcode) > Integer.valueOf(mDatas.get(position).getDespensing_exec().get(i).getBarcode()))
//                            || Integer.valueOf(barcode)==-1)&& mDatas.get(position).getDespensing_exec().get(i).getExecempid() == null)
//                    {
//                        barcode = mDatas.get(position).getDespensing_exec().get(i).getBarcode();
//                    }
//                }
//                activity.ExecuteOrder_inner(false,barcode,position);
//            }
//        });

        return convertView;
    }

    class ViewHolder{
        ListView injectionlist_list;
        Button injectionlist_statu_btn;
        TextView injectionlist_statu_tv,injectionlist_plan_usage,injectionlist_count;
        RelativeLayout injectionlist_ll;
        CheckBox radiobutton;
    }
}
